//
//  ViewController.m
//  01-掌握-corelocation框架的基本使用—定位
//
//  Created by J on 16/2/27.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
@property(nonatomic,strong)CLLocationManager* manager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   }

- (CLLocationManager *)manager{
    
    if (!_manager) {
        //1创建位置管理者
        CLLocationManager* manager = [CLLocationManager new];
        self.manager = manager;
        //1.1 代理 通知 block
        manager.delegate = self;
        
        //每隔多远定位一次
//        [manager setDistanceFilter:100.0];
        
//        kCLLocationAccuracyBestForNavigation//最适合导航
//        kCLLocationAccuracyBest;//最好的
//        kCLLocationAccuracyNearestTenMeters;/10m
//        kCLLocationAccuracyHundredMeters;//100m
//        kCLLocationAccuracyKilometer;//1000m
//        kCLLocationAccuracyThreeKilometers;//3000m
        //精确度越高 越耗电 定位时间越长
        
        if( [[UIDevice currentDevice].systemVersion floatValue]>=8.0){
            
            //        [manager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
            [manager requestAlwaysAuthorization];//前后台定位授权
//            [manager requestWhenInUseAuthorization];//前台定位授权
//            manager.allowsBackgroundLocationUpdates =YES;
        }
        
        //如果能影响酒调用
//        if ([manager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
//            
//            //        [manager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
//            [manager requestAlwaysAuthorization];//前后台定位授权
        
        
        // 5.iOS9新特性：将允许出现这种场景：同一app中多个location manager：一些只能在前台定位，另一些可在后台定位（并可随时禁止其后台定位）。
//        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
//            manager.allowsBackgroundLocationUpdates = YES;
//        }
        //
//            //        [manager requestWhenInUseAuthorization];//前台定位授权
//        }
        
        
        
    }
    
    return _manager;
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  
    //2.使用位置管理者 开始更新用户位置
    [self.manager startUpdatingLocation];
    
    
    
    //        kCLLocationAccuracyBestForNavigation//最适合导航
    //        kCLLocationAccuracyBest;//最好的
    //        kCLLocationAccuracyNearestTenMeters;/10m
    //        kCLLocationAccuracyHundredMeters;//100m
    //        kCLLocationAccuracyKilometer;//1000m
    //        kCLLocationAccuracyThreeKilometers;//3000m
    //这个方法后 精确度从300到最好 过程中超时就会通过代理回调 需要实现didFailWithError协议方法
    [self.manager requestLocation];
}


#pragma mark --CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    
    NSLog(@"%@",error);
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{//关闭定位  和重不定位都显示为 拒绝 kCLAuthorizationStatusDenied 所以需要判断区分
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"定位被关闭");
    }else if (status == kCLAuthorizationStatusDenied){
        NSLog(@"定位拒绝");
        
    }
    NSLog(@"%zd",status);
    
    
}



///  更新到位置后调用
///
///  @param manager   位置管理者
///  @param locations 位置数组
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{

    NSLog(@"定位到了");
    //拿到位置,做一些业务逻辑操作
    
    //停止更新
//    [manager stopUpdatixngLocation];
    
    
    
}



@end
